package org.scalpel.mainApps.HHObjects;

import ioio.lib.api.IOIO;
import ioio.lib.api.exception.ConnectionLostException;

import org.scalpel.common.HHObject;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

/**
 * 
 * @author Jerome Monceaux
 * @version 1.0
 * 
 * This Module is designed to manage wheel motor speed.
 * It uses Locomotion and Odometry to compute speed and position of the robot.
 *    
 *
 */
public class ModUpdateTime extends HHObject {


  
  // Still need to clean robot and field space 
  int fOffsetX = 500;
  int fOffsetY = 380;
  
  private int fTimeUpdateInMs;
  private long fTimeInit;
  private long fTimeCurrentUpdate;
  private long fTimeLastUpdate;
  private double fTimeDurationInS;
  
  boolean fPlotTime = true;
  
  ModUpdateTime(String name) {
    super(name);
  }

  /**
   * Inherit from HHmodule, call systematically even if the hardware is not connected
   */
  public void initIOIO(boolean isSimulation, IOIO ioio) throws ConnectionLostException{

    fTimeDurationInS=0;
    fTimeUpdateInMs=0;
    fTimeCurrentUpdate=0;
    fTimeInit=System.currentTimeMillis();
    fTimeLastUpdate=fTimeInit;
  }
  

  /**
   * Inherit from HHmodule, call systematically even if the hardware is not connected
   */
  public void updateIOIO() throws ConnectionLostException{
    fTimeCurrentUpdate=System.currentTimeMillis();
    fTimeDurationInS=(double)(fTimeCurrentUpdate-fTimeInit)/1000.0;
    fTimeUpdateInMs=(int) (fTimeCurrentUpdate-fTimeLastUpdate);
    fTimeLastUpdate=fTimeCurrentUpdate;
  }
  
  /**
   * set the real speed plot visible or not
   * @param onoff
   */
  public void setTimePlotVisible( boolean onoff){
    fPlotTime = onoff;
  }
  
  /**
   * get the real speed plot visible
   * @param onoff
   */
  public boolean getTimePlotVisible(){
    return fPlotTime;
  }
  
  /**
   * Inherit from HHModule. Call to refresh the draw.
   */
  public void onDraw(Canvas canvas, Paint robotPaint){
    
    
    ///////////// Draw the real speed plot ///////////// 
    // if needed...
    if( !fPlotTime)
      return;
    
    int px = fOffsetX;
    int py = fOffsetY;
    
    canvas.save(); //================>
    canvas.translate(px, py);
    robotPaint.setColor(Color.WHITE);

    canvas.drawText(("Time elapsed : "+Double.toString(fTimeDurationInS))+" s", 0, 0, robotPaint);
    canvas.drawText(("Update time : "+Integer.toString(fTimeUpdateInMs))+" ms", 150, 0, robotPaint);

    canvas.restore(); //<================
  
  }

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		
	}
}

